package org.skatiger.jrove.renderer.html;

import java.io.IOException;

import org.skatiger.jrove.ResponseWriter;
import org.skatiger.jrove.ViewContext;
import org.skatiger.jrove.component.JroveAttr;
import org.skatiger.jrove.component.UIComponent;
import org.skatiger.jrove.renderer.Html;
import org.skatiger.jrove.renderer.HtmlRendererBase;
import org.skatiger.jrove.renderer.RendererUtils;

public class HtmlLinkRenderer extends HtmlRendererBase {

    @Override
    public void encodeBegin(ViewContext context, UIComponent component)
            throws IOException {
        super.encodeBegin(context, component);
        boolean renderAnchor = (Boolean) component.getAttributes().get(
                "renderedAnchor");
        ResponseWriter writer = context.getResponseWriter();
        if (renderAnchor) { 
            writer.startElement(Html.ANCHOR_ELEM, component);
            renderId(context, component);
            String href = calculateUrl(context, component,JroveAttr.HREF_ATTR);
            writer.writeURIAttribute(Html.HREF_ATTR, href, JroveAttr.HREF_ATTR);
            RendererUtils.renderHtmlStandardAttributes(writer, component,
                    Html.ANCHOR_ELEM_OPTIONAL_ATTRIBUTES_WITH_OUT_HREF);
        } else {
            writer.startElement(Html.SPAN_ELEM, component);
        }
    }

    @Override
    public void encodeEnd(ViewContext context, UIComponent component)
            throws IOException {
        super.encodeEnd(context, component);
        boolean renderAnchor = (Boolean) component.getAttributes().get(
                "renderedAnchor");
        ResponseWriter writer = context.getResponseWriter();

        if (renderAnchor) {
            writer.endElement(Html.ANCHOR_ELEM);
        } else {
            writer.endElement(Html.SPAN_ELEM);
        }
    }
}
